let puppeteer = require('puppeteer');
let Film = require('../models/film.js');
let rp = require('request-promise');
require('../libs/db.js')();
let {fetchResourcesToCdn} = require('../qiniu');

/**
 *
 * @param {获取电影详情} id
 */
let getFilmDetail = async (id) => {
  let options = {
    uri: `https://api.douban.com/v2/movie/subject/${id}`
  };
  let fDetail = await rp(options);
  return fDetail;
};

/**
 * 爬取基本数据
 */
let getFilm = async () => {
  let tag = '';
  let url = `https://movie.douban.com/cinema/nowplaying/shenzhen/`;
  // let url = `https://movie.douban.com/explore#!type=movie&tag=${tag}&sort=recommend&page_limit=20&page_start=0`;

  // 创建一个浏览器
  let browser = await puppeteer.launch({
    ignoreHTTPSErrors: true,
    timeout: 10000,
    devtools: false,
    headless: false
  });

  // 打开一个新页面
  let page = await browser.newPage();

  console.log(`开始爬取....`);
  // 加载对应的页面
  await page.goto(url);

  page.on('console', (msg) => {
    for (let i = 0; i < msg.args().length; i++) {
      console.log(`${i}:${msg.args()[i]}`);
    }
  });

  // 等待页面上的加载更多(class是more)出现
  await page.waitFor(2000);
  await page.waitForSelector('.more');

  for (let i = 0; i < 3; i++) {
    // 等待2s
    await page.waitFor(2000);
    // 点击加载更多
    await page.click('.more');
  }

  // 执行js脚本
  let brr = await page.evaluate(() => {
    let $ = window.$;
    let doms = $('.list').find('.item');
    let arr = [];
    doms.each((index, elem) => {
      arr.push({
        poster: $(elem).find('img').attr('src').replace('s_ratio_poster', 'l_ratio_poster'), // 获取海报图,抓取到的书小图,换成高清大图
        name: $(elem).find('p').text().trim().split('\n')[0], // 获取title
        rate: Number($(elem).find('p strong').text().trim()), // 获取评分
        id: $(elem).find('.cover-wp').data('id') // 获取豆瓣id
      });
    });
    return arr;
  });

  // 关闭浏览器
  await browser.close();
  return brr;
};

/**
 *
 * 数据库添加信息
 */
let inserIntoDb = async (film, item) => {
  let posterUrl = '';
  if (item.poster) {
    posterUrl = await fetchResourcesToCdn(item.poster);
  }
  await Film.create({
    doubanId: item.id,
    title: item.title,
    poster: posterUrl,
    rate: item.rate,
    year: film.year,
    countries: film.countries,
    genres: film.genres,
    actors: film.casts || [].map(i => ({avatar: i.avatars, name: i.name})),
    summary: film.summary,
    directors: film.directors || [].map(i => ({avatars: i.avatars, name: i.name})),
    ratings_count: film.ratings_count,
    collect_count: film.collect_count,
    wish_count: film.wish_count,
    aka: film.aka,
    original_title: film.original_title
  });
};

(async () => {
  let movies = await getFilm();
  for (let i = 0; i < movies.length; i++) {
    let item = movies[i];
    let f = await Film.findOne({doubanId: item.id});
    if (f && Object.keys(f).length > 0) {
      continue;
    } else {
      let film = await getFilmDetail(item.id);
      await inserIntoDb(JSON.parse(film), item);
    }
    if (i === movies.length - 1) {
      process.exit(0);
    }
  }
})();
